<template>
  <div>
    <div v-if="!viewMode">
      <input type="text" :value="value" style="display: none" />
      <el-select
        v-model="selected"
        :clearable="clearable"
        :size="size"
        :placeholder="placeholder"
        @change="handleItemChange"
      >
        <el-option
          v-for="item in cateList"
          :key="item.userName"
          :label="item.realName"
          :value="item.userName"
        ></el-option>
      </el-select>
    </div>
    <div v-else>{{ selectedName }}</div>
  </div>
</template>

<script>
import { getStaff } from '@/common/api/auth'
export default {
  components: {},
  props: {
    value: {
      type: String,
      default: '',
    },
    size: {
      type: String,
      default: '',
    },
    placeholder: {
      type: String,
      default: '选择角色',
    },
    clearable: {
      type: Boolean,
      default: false,
    },
    viewMode: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      selected: null,
      selectedName: '',
      cateList: [],
      cateMap: {},
    }
  },
  computed: {},
  watch: {
    value(val) {
      this.selected = val
      this.selectedName = this.cateMap[this.selected] || ''
    },
  },
  created() {
    this.getData()
  },
  mounted() {},
  methods: {
    getData() {
      getStaff().then(res => {
        this.cateList = (res && res.data) || []
        this.cateList.forEach(item => {
          this.cateMap[item.userName] = item.realName
        })
        this.selected = this.value
        this.selectedName = this.cateMap[this.selected] || ''
      })
    },
    handleItemChange(val) {
      if (!this.autoChange) {
        this.$emit('input', val)
        this.$emit('change', val)
      }
      // 需要返回label时候取消注释
    },
    init(val) {
      this.selected = val
    },
    getSelected() {
      return this.selected
    },
    getSelectedName() {
      return this.selectedName
    },
  },
}
</script>

<style scoped></style>
